package com.leyou.search.mq;

import com.leyou.common.constants.MQConstants;
import com.leyou.search.service.SearchService;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class ItemListener {
    @Autowired
    private SearchService searchService;

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = MQConstants.QueueConstants.SEARCH_ITEM_UP, durable = "true"),
            exchange = @Exchange(name ="jhj", type = ExchangeTypes.TOPIC),
            key = MQConstants.RoutingKeyConstants.ITEM_UP_KEY
    ))
    public void listenitemUp(Long spuId){
        if (spuId != null){
            searchService.saveGoodById(spuId);
        }
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = MQConstants.QueueConstants.SEARCH_ITEM_DOWN, durable = "true"),
            exchange = @Exchange(name ="jhj", type = ExchangeTypes.TOPIC),
            key = MQConstants.RoutingKeyConstants.ITEM_DOWN_KEY
    ))
    public void listenitemDown(Long spuId){
        if(spuId != null){
            searchService.deleteGoodById(spuId);
        }
    }
}
